package dimension.starry.block.machine.simple.castTable;

import dimension.starry.StarryDimension;
import dimension.starry.block.machine.simple.SimpleMachineRegister;
import dimension.starry.block.typeInterface.MachineTileEntity;
import dimension.starry.constants.ItemNbtTag;
import dimension.starry.item.ItemRegisterManager;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

/**
 * @author 150149
 */
public class CastTableTileEntity extends MachineTileEntity {

    private static final Text TITLE = Text.translatable("block." + StarryDimension.MOD_ID + ".cast_table");

    public CastTableTileEntity(BlockPos pos, BlockState state) {
        super(SimpleMachineRegister.CAST_TABLE.getBlockEntityType(), pos, state, 4, TITLE, 800);
    }

    private static Item getOutput(ItemStack input) {
        String descriptionId = input.getOrCreateNbt().getString(ItemNbtTag.STONE_POT_OUT);
        if (descriptionId.equals(Items.RAW_COPPER.getTranslationKey())) {
            return Items.COPPER_INGOT;
        } else if (descriptionId.equals(ItemRegisterManager.TIN.getItem().getTranslationKey())) {
            return ItemRegisterManager.TIN.getItem();
        }
        return null;
    }

    @Override
    public ScreenHandler createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new CastTableContainer(
            SimpleMachineRegister.CAST_TABLE.getScreenHandlerType(), sycID, inventory, getPos());
    }

    @Override
    public void tick(
        World world, BlockPos pos, BlockState state1, MachineTileEntity blockEntity
    ) {
        if (world == null || world.isClient()) {
            return;
        }

        ItemStack potItemStack = this.inventory.getStack(0);
        ItemStack waterItemStack = this.inventory.getStack(1);

        if (potItemStack.isEmpty() || waterItemStack.isEmpty()) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (!waterItemStack.getItem().equals(ItemRegisterManager.WATER_STONE_POT.getItem())) {
            this.setMachineStatus(false);
            this.resetProgress();
        }

        if (potItemStack.getItem().equals(ItemRegisterManager.LAVA_STONE_POT.getItem())) {
            if (potItemStack.getNbt() == null) {
                this.setMachineStatus(false);
                this.resetProgress();
            } else if (getOutput(potItemStack) == null) {
                this.setMachineStatus(false);
                this.resetProgress();
            } else if (this.getProgress() >= this.getConsumeTime()) {
                ItemStack itemStack = new ItemStack(getOutput(potItemStack));
                if (this.inventory.canInsert(itemStack)) {
                    this.inventory.addStack(itemStack);
                    this.inventory.addStack(new ItemStack(ItemRegisterManager.STONE_POT.getItem(), 2));
                    potItemStack.setCount(potItemStack.getCount() - 1);
                    waterItemStack.setCount(waterItemStack.getCount() - 1);
                    this.resetProgress();
                }
            } else if (!waterItemStack.isEmpty()) {
                this.addProgress();
                this.setMachineStatus(true);
            }
        } else if (potItemStack.getItem().equals(ItemRegisterManager.HOT_IRON.getItem())) {
            if (this.getProgress() >= this.getConsumeTime()) {
                ItemStack itemStack = new ItemStack(ItemRegisterManager.STEEL.getItem());
                if (this.inventory.canInsert(itemStack)) {
                    this.inventory.addStack(itemStack);
                    this.inventory.addStack(new ItemStack(ItemRegisterManager.STONE_POT.getItem()));
                    potItemStack.setCount(potItemStack.getCount() - 1);
                    waterItemStack.setCount(waterItemStack.getCount() - 1);
                    this.resetProgress();
                }
            } else if (!waterItemStack.isEmpty()) {
                this.addProgress();
                this.setMachineStatus(true);
            }
        }
    }

}
